local skill = fk.CreateSkill{
  name = "tea__xuzhong",
}
Fk:loadTranslationTable{
  ["tea__xuzhong"] = "蓄众",
  [":tea__xuzhong"] = "出牌阶段，你可以观看牌堆顶三张牌，然后获得其中点数与你手牌均不同且在点数最大与最小之间的牌。若你未以此法获得牌"..
  "，你失去1点体力并获得这些牌，然后此技能本回合失效。",
  ["#tea__xuzhong-prompt"] = "蓄众：你可以观看牌堆顶三张牌，有机会获得其中的牌",
  ["@@tea__xuzhong_mark-turn"] = "蓄众失效",
  ["#tea__xuzhong-viewcards"] = "蓄众：观看牌堆顶三张牌",
}

---@type ActiveSkillSpec
local xuzhong = {
  anim_type = "special",
  prompt = "#tea__xuzhong-prompt",
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  can_use = function(self, player)
    return true
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cids = {}
    for i = 1, 3 do
      table.insert(cids, room.draw_pile[i])
    end
    if #cids > 0 then
      room:viewCards(player, {
        cards = cids,
        skill_name = skill.name,
        prompt = "#tea__xuzhong-viewcards"
      })
      local hands = table.map(player:getCardIds("h"), function(id)
        return Fk:getCardById(id).number
      end)
      local views = table.simpleClone(cids)
      table.sort(views, function (a, b)
        return Fk:getCardById(a).number < Fk:getCardById(b).number
      end)
      local middle_numbers = table.filter(views, function(id)
        return Fk:getCardById(id).number > Fk:getCardById(views[1]).number and Fk:getCardById(id).number < Fk:getCardById(views[#views]).number
      end)
      local cards = table.filter(middle_numbers, function (id)
        return not table.contains(hands, Fk:getCardById(id).number)
      end)
      if #cards > 0 then
        room:obtainCard(player, cards, false, fk.ReasonPrey)
      else
        room:loseHp(player, 1 ,skill.name)
        if not player.dead then
          room:obtainCard(player, cids, false, fk.ReasonPrey)
        end
        room:invalidateSkill(player, skill.name, "-turn", skill.name)
      end
    end
  end,
}
skill:addEffect("active", xuzhong)
return skill